package org.dromara.demo.designPattern.装饰器模式.service;

import org.dromara.demo.designPattern.装饰器模式.decorator.SsoDecorator;
import org.dromara.demo.designPattern.装饰器模式.interceptor.HandlerInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 在具体的装饰类实现中，继承了装饰类 SsoDecorator，现在可以扩展方法
 * preHandle的功能。在具体的实现代码中可以看到，这里只关心扩展部分的功
 * 能，同时不会影响原有类的核心服务，也不会因为使用继承方式而导致出现多
 * 余子类，增加了整体的灵活性。
 */

public class LoginSsoDecorator2 extends SsoDecorator {
    private Logger logger = LoggerFactory.getLogger(LoginSsoDecorator2.class);
    private static Map<String, String> authMap = new ConcurrentHashMap<String, String>();

    static {
        authMap.put("huahua", "queryUserInfo");
        authMap.put("doudou", "queryUserInfo");
    }

    public LoginSsoDecorator2(HandlerInterceptor handlerInterceptor) {
        super(handlerInterceptor);
    }

    @Override
    public boolean preHandle(String request, String response,
                             Object handler) {
        boolean success = super.preHandle(request, response, handler);
        if (!success) return false;
        String userId = request.substring(8);
        String method = authMap.get(userId);

        logger.info("模拟单点登录方法访问拦截校验：{} {}", userId, method);
         // 模拟方法校验
        return "queryUserInfo".equals(method);
    }
}
